﻿using UnityEngine;
using System.Collections;
using System.IO;
using Elixis;
using Elixis.EncryptionOptions;
public class Image_Byte : MonoBehaviour {
    private static AESEncryptor fAesEncrypt = new AESEncryptor( "bytes", AESBits.BITS256 );
    public UILabel INFilePath;
    public UILabel InnewFilePath;
    //public UILabel INtianjiaString;


    //public string FilePath;
    //public string newFilePath;
    //public string tianjiaString;


    //public void StartOnclike()
    //{
    //    string imagefilePath = INFilePath.text;     

    //    byte[] byteData = GetPictureData(imagefilePath);
    //    SavePictureData(byteData);
    //}
    public byte[] bytedata;
    public string newnamebyte;
    public void StartOnclike()
    {
        string imagefilePath = INFilePath.text;
        string bytesfilePath = InnewFilePath.text;
        //string tianjiaString = INtianjiaString.text;

        string[] files = Directory.GetFiles(imagefilePath);

        DirectoryInfo folder = new DirectoryInfo(imagefilePath);
        //FileInfo[] infos = folder.GetFiles("*.jpg");
        FileInfo[] infos = folder.GetFiles();
        //FileInfo[] bytes = folder.GetFiles("*.bytes");
        for (int i = 0; i < infos.Length; i++)
        {        
                        
            int info = (int)Mathf.Sqrt(infos.Length);
            string newname = infos[i].Name;
           
            newnamebyte = newname;
            string imagename = imagefilePath +@"\"+ infos[i].Name;

            GetPictureData(imagename);
            AesEncrypt( bytedata );
            SavePictureData(bytedata);
            //File.Move(files[i], newname);
            
            
        }

    }



    public byte[] GetPictureData(string imagefilePath)
    {

        FileStream fs = new FileStream(imagefilePath, FileMode.Open);
        byte[] byteData = new byte[fs.Length];
        fs.Read(byteData, 0, byteData.Length);
        fs.Close();        
        bytedata = byteData;
        return byteData;
        
    }
    public byte[] AesEncrypt( byte[] bytes )
    {
        byte[] AesByteData = fAesEncrypt.Encrypt( bytes );
        bytedata = AesByteData;
        return bytedata;
    }
    public void SavePictureData(byte[] byteData)
    {
        //string bytesfilePath = InnewFilePath.text+ @"\"+ newnamebyte + ".bytes";     
        string bytesfilePath = InnewFilePath.text + (@"\" + newnamebyte).Remove(newnamebyte.Length-3,4) + ".bytes";
        
        File.Create(bytesfilePath).Write(byteData, 0, byteData.Length);

    }

    public void Exit()
    {
        Application.Quit();
    }

    //void Start () {
    //File.
    //string[] files=Directory.GetFiles(FilePath);

    // DirectoryInfo folder = new DirectoryInfo(FilePath);
    // FileInfo[] infos = folder.GetFiles("*.jpg");
    //// for (int i = 0; i <= files.Length; i++)
    //// {
    ////     FileStream file = File.Create(files[i]);

    // //     File.
    // // }

    // //第二种方法 

    // for (int i=0;i<infos.Length;i++)
    // {

    //     int info = (int)Mathf.Sqrt(infos.Length);

    //     //string oldname =  files[i].Replace(@"\\",@"\");

    //     //string newname = newFilePath + @"\" + tianjiaString + infos[i].Name;
    //     string newname = newFilePath + @"\" + tianjiaString + i / info + "_" + i % info+".jpg";
    //     //File.Move(oldname, newname);
    //     File.Move(files[i], newname);
    //     //file.MoveTo(FilePath + @"\\" + newname);
    // }

    //  }

    // Update is called once per frame
    void Update()
    {

    }
    //public static FileStream Open(string path, FileMode mode)

}
